home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PACKET / CBBS60SO.ZIP / MBLOG.C < prev    next >
Text File  |  1988-04-19  |  6KB  |  288 lines

  1.  
  2. /*
  3.  *  MBLOG.C - 3/01/87 - Logging and Monitoring.
  4.  */
  5.  
  6. #include "mb.h"
  7.  
  8. #define ln_jstr (ln_callp + ln_time)
  9.  
  10. char *lgfile;
  11. FILE *lfl;
  12.  
  13. char *monfile;
  14.  
  15. char *hrdfile, *hrd;
  16. int  hrdmax, hrdn;
  17.  
  18. /*
  19.  *  Clean the log file.
  20.  *  Force drain of buffers, update of directory item.
  21.  */
  22.  
  23. clnlog()
  24. {
  25.   if (s_param & s_log_on)
  26.   {
  27.     fclose(lfl);
  28.     lfl = fopen(lgfile, "a+");
  29.   }
  30. }
  31.  
  32. clslog()
  33. {
  34.   if (s_param & s_log_on) fclose (lfl);
  35. }
  36.  
  37. /*
  38.  *  Open the log file.
  39.  */
  40.  
  41. opnlog()
  42. {
  43.   if (s_param & s_log_on)
  44.     if ((lfl = fopen(lgfile, "a+")) is NULL) { nofile(lgfile); exit(1); }
  45. }
  46.  
  47. /*
  48.  *  Log an event.
  49.  */
  50.  
  51. log(event, fn, sfn, text)
  52. char event, fn, sfn, *text;
  53. {
  54.  
  55. /*
  56.  *  Do we log this type of event?
  57.  */
  58.  
  59.   if (!(s_param & s_log_on)) return;
  60.  
  61.   if (port->dev is p_console) if (!(s_param & s_log_loc))
  62.   if (!((event is 'M') and ((fn is 'S') or (fn is 'M')))) return;
  63.  
  64.   switch(event)
  65.   {
  66.     case 'G' : if (!(s_param & s_log_gate)) return; break;
  67.     case 'M' : if (!(s_param & s_log_msg))  return; break;
  68.     case 'F' : if (!(s_param & s_log_file)) return; break;
  69.   }
  70.  
  71. /*
  72.  *  Yes, so log it.
  73.  */
  74.  
  75.   curtim();
  76.   fprintf(lfl, "%c%s%s%c%c %s\n", event, l_date, l_time, fn, sfn, text);
  77. }
  78.  
  79. /*
  80.  *  Monitoring ...
  81.  */
  82.  
  83. clsmon()
  84. {
  85.   register short  i;
  86.   register char  *c;
  87.   register PORTS *p;
  88.  
  89. /*
  90.  *  Write out the heard log.
  91.  */
  92.  
  93.   if ((port->fl = fopen(hrdfile, "w")) isnt NULL)
  94.   {
  95.     sort(hrd, hrdn, ln_call, tmp->scr);
  96.     for (i = 0, c = hrd; i < hrdn; i++, c += ln_call)
  97.       fprintf(port->fl, "%6.6s\n", c);
  98.     fclose(port->fl);
  99.   }
  100.  
  101. /*
  102.  *  Write out the J list for each port.
  103.  */
  104.  
  105.   if ((port->fl = fopen(monfile, "w")) isnt NULL)
  106.   {
  107.     for (p = porthd; p isnt NULL; p = p->next)
  108.     {
  109.       for (i = 0, c = p->heard; i < p->nhrd; i++, c += ln_jstr)
  110.       fprintf (port->fl, "%13.13s\n", c);
  111.       fprintf (port->fl, "*** EOF\n");
  112.     }
  113.     fprintf (port->fl, "%s%s\n", l_date, l_time);
  114.     fclose (port->fl);
  115.   }
  116. }
  117.  
  118. /*
  119.  *  Open "heard log" and "monitor log".
  120.  */
  121.  
  122. opnmon()
  123. {
  124.   register char *i, *j, *st;
  125.   register PORTS *p;
  126.  
  127. /*
  128.  *  Open the "heard list" file.
  129.  */
  130.  
  131.   if ((hrd = (char *)malloc(ln_call * hrdmax)) is NULL) errall();
  132.  
  133.   hrdn = 0;
  134.  
  135.   if ((port->fl = fopen(hrdfile, "r")) isnt NULL)
  136.   {
  137.     i = hrd;
  138.     while((hrdn < hrdmax) and (fgets (port->line, linelen, port->fl) isnt NULL))
  139.     {
  140.       pcall(i, port->line);
  141.       hrdn++; i += ln_call;
  142.     }
  143.     fclose(port->fl);
  144.     sprintf(port->line, "%d calls in %s", hrdn, hrdfile); outstr(port->line);
  145.     if (hrdn is hrdmax) outstr(" (File is full.)");
  146.     outchar('\n');
  147.   }
  148.  
  149. /*
  150.  *  Open the monitor file and read the J list for each port.
  151.  */
  152.  
  153.   if ((port->fl = fopen(monfile, "r")) isnt NULL)
  154.   {
  155.     for (p = porthd; p isnt NULL; p = p->next)
  156.     {
  157.       st = fgets (port->line, linelen, port->fl);
  158.       j = p->heard;
  159.       while (!iseof(port->line) and (st isnt NULL))
  160.       {
  161.         if (p->nhrd < p->maxhrd)
  162.         {
  163.           for (i = port->line; i < port->line + ln_jstr;) *j++ = *i++;
  164.           p->nhrd++;
  165.         }
  166.         st = fgets (port->line, linelen, port->fl);
  167.       }
  168.     }
  169.     fclose(port->fl);
  170.   }
  171. }
  172.  
  173. /*
  174.  *  Add call in monitored packet to J list of current port.
  175.  */
  176.  
  177. monitor()
  178. {
  179.   register short l1, l2;
  180.  
  181.   if (port->heard is NULL) return;
  182.  
  183.   for (l1 = 0; l1 < ln_callp; l1++) if (port->line[l1] is '>')
  184.   {
  185.     if (l1 < 4) return;
  186.  
  187.     port->line[l1] = '\0';
  188.  
  189.     for (l2 = 1; l2 < 10; l2++)
  190.     if ((port->line[l1 + l2] is ':') or
  191.         (port->line[l1 + l2] is ',') or
  192.         (port->line[l1 + l2] is ' ') or
  193.         (port->line[l1 + l2] is '\0'))
  194.     {
  195.       if (l2 > 4) { addcall(port->line, port); addhrd(); }
  196.       return;
  197.     }
  198.     return;
  199.   }
  200. }
  201.  
  202. /*
  203.  *  Add call to heard list.
  204.  */
  205.  
  206. addhrd()
  207. {
  208.   register int i;
  209.   register char *p;
  210.  
  211.   if (hrdn >= hrdmax) return;
  212.   pcall(tmp->scr, port->line);
  213.  
  214.   for (i = 0, p = hrd; i < hrdn; i++, p += ln_call)
  215.   if (matchn(tmp->scr, p, ln_call)) return;
  216.  
  217.   strncpy(hrd + (ln_call * hrdn++), tmp->scr, ln_call);
  218. }
  219.  
  220. /*
  221.  *  Add call to J list.
  222.  */
  223.  
  224. addcall(cp, p)
  225. char *cp;
  226. PORTS *p;
  227. {
  228.   register char  *k;
  229.   register short i;
  230.   register char  *sk;
  231.   register short si;
  232.  
  233.   ljsf(tmp->scr, cp, ln_callp);
  234.  
  235. /*
  236.  *  Check if this call already in list for this port.
  237.  */
  238.  
  239.   si = -1;
  240.   for (i = 0, k = p->heard; i < p->nhrd; i++, k += ln_jstr)
  241.   if (matchn(tmp->scr, k, ln_callp)) { si = i; sk = k; }
  242.  
  243. /*
  244.  *  If not already in the list ...
  245.  */
  246.  
  247.   if (si is -1)
  248.   {
  249.     si = p->nhrd; sk = p->heard + ln_jstr * p->nhrd;
  250.     if (p->nhrd < p->maxhrd) p->nhrd++; else { si--; sk -= ln_jstr; }
  251.   }
  252.  
  253. /*
  254.  *  Put call at top of list for this port.
  255.  */
  256.  
  257.   for (; si; si--, sk -= ln_jstr) strncpy (sk, sk - ln_jstr, ln_jstr);
  258.   curtim();
  259.   strncpy(p->heard + ln_callp, l_time, ln_time);
  260.   strncpy(p->heard, tmp->scr, ln_callp);
  261. }
  262.  
  263. /*
  264.  *  J command, print list of calls heard.
  265.  */
  266.  
  267. prthrd()
  268. {
  269.   register short i;
  270.   register char *j;
  271.   register PORTS *p;
  272.  
  273.   if (port->opt2 is ' ') { shports(); return; }
  274.  
  275.   if ((p = findport(port->opt2)) is NULL) { prtx(mnport); return; }
  276.  
  277.   sprintf(tmp->scr, "Port %c, %s\n", p->id, p->name);
  278.   outstr(tmp->scr);
  279.  
  280.   if (!(p->nhrd)) { prtx(mfind); return; }
  281.  
  282.   for (i = 0, j = p->heard; i < p->nhrd; i++, j += ln_jstr)
  283.   {
  284.     sprintf(tmp->scr, "%9.9s %4.4s\n", j, j + ln_callp);
  285.     outstr(tmp->scr);
  286.   }
  287. }
  288.